package cn.tedu.boot22.controller;

import cn.tedu.boot22.entity.Emp;
import cn.tedu.boot22.utils.DBUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.jnlp.PersistenceService;
import java.sql.*;
import java.util.ArrayList;

@RestController
//使用此注解取代Controller 相当于在当前类的
// 每一个方法上添加@ResponseBody注解
public class EmpController {
    @RequestMapping("/add")
    @ResponseBody
    public String add(Emp emp){
        System.out.println("emp = " + emp);
        try (Connection conn=DBUtils.getConn()){
            String sql="insert into myemp values(null,?,?,?)";
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setString(1,emp.getName());
            ps.setString(2,emp.getJob());
            ps.setInt(3,emp.getAge());
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "添加完成";
    }
    @RequestMapping("/select")
    @ResponseBody
    public String select(){
        //此处查询员工信息代码
        //此处集合用来装查询到的所有员工信息
        ArrayList<Emp> list=new ArrayList<>();
        try (Connection conn=DBUtils.getConn()){
            String sql="select * from myemp";
            Statement s=conn.createStatement();
            ResultSet rs=s.executeQuery(sql);
            //遍历结果集
            while (rs.next()){
                int id=rs.getInt(1);
                String name=rs.getString(2);
                String job=rs.getString(3);
                int age=rs.getInt(4);
                //把查询到的多条数据封装到Emp对象中
                Emp emp=new Emp(id,name,job,age);
                //把对象装进集合
                list.add(emp);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        String html="<table border=1>";
        html+="<caption>员工列表</caption>";
        html+="<tr><th>id</th><th>名字</th><th>工作</th><th>年龄</th><th>操作</th></tr>";
        //此处把员工信息添加到table表格中
        //遍历集合中的每一个员工对象
        for (Emp e:list){
            html+="<tr>";
            html+="<td>"+e.getId()+"</td>";
            html+="<td>"+e.getName()+"</td>";
            html+="<td>"+e.getJob()+"</td>";
            html+="<td>"+e.getAge()+"</td>";
            html+="<td><a href='/delete?id="+e.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html;
    }
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(int id){
        System.out.println("id = " + id);
        try (Connection conn=DBUtils.getConn()){
            String sql="delete from myemp where id=?";
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setInt(1,id);
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "删除完成<a href='/'>回到首页</a>";
    }
}
